<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>height</title>
    <script src = 'jquery-1.12.4.min.js'></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .bord{
            border: 1px dashed #ee2602;
            width: 90%;
            margin: 20px auto;
        }
        .title{
            font-size: 20px;
            font-weight: bold;
            margin: 20px;
        }
        .introduce{
            font-size: 16px;
            margin: 0 0 20px 50px;
        }
        .red{
            color: red;
        }
    </style>
    <script>
        jQuery(function ($) {
            $('#mybtn').click(function () {
                var a = $("#a");
                var offset = a.offset();

                //获取xxx元素的html代码内容
                //$('#xxx').html() 的结果是   代码内容的字符串
                //alert($('#xxx').html());

                //设置xxx元素的内容(以html的方式来设置)
                //返回的结果仍然是$('#xxx')这个jq对象
                //alert($('#xxx').html('...'));


                a.html( "left: " + offset.left + " px, top: " + offset.top + " px");
            })
        });
    </script>
</head>
<body>
<div class="bord">
    <div class="title">height</div>
    <div class="introduce">获取匹配元素在当前视口的相对偏移。</div>
</div>
<div class="bord">
    <div class="title">演示效果</div>
    <div class="introduce">
        <div id="a" style="width: 150px;height: 50px;background-color: #3388ff"></div>
        <br/>
        <button id="mybtn">获取div元素在当前视口的相对偏移。</button>
    </div>

</div>
<div class="bord">
    <div class="title">代码</div>
    <div class="introduce">
        <pre>
&lt;script&gt;
    jQuery(function ($) {
            var a = $("#a");
            var offset = a.<a class="red">offset()</a> ;
            a.html( "left: " + offset.left + " px, top: " + offset.top + " px");
        });
&lt;/script&gt;
            <br/>
&lt;div class="introduce"&gt;
    &lt;div id="a" style="width: 150px;height: 50px;background-color: #3388ff"&gt;&lt;/div&gt;
    &lt;br/&gt;
    &lt;button id="mybtn"&gt;获取div元素在当前视口的相对偏移。&lt;/button&gt;
&lt;/div&gt;
        </pre>
    </div>
</div>
</body>
</html>